#!/usr/bin/env python
__license__ = 'GPL v3'
__copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
__docformat__ = 'restructuredtext en'

'''
espn.com
'''
import re
from calibre.web.feeds.news import BasicNewsRecipe
from calibre.ptempfile import TemporaryFile


def classes(classes):
    q = frozenset(classes.split(' '))
    return dict(attrs={
        'class': lambda x: x and frozenset(x.split()).intersection(q)})


class ESPN(BasicNewsRecipe):

    title = 'ESPN'
    description = 'Sports news'
    __author__ = 'Kovid Goyal and Sujata Raman'
    language = 'en'
    no_stylesheets = True

    use_embedded_content = False
    remove_javascript = True
    needs_subscription = 'optional'

    keep_only_tags = [
        classes('article-header article-body'),
    ]
    remove_tags = [
        classes('ad-slot article-social'),
    ]

    feeds = [
        ('Top Headlines', 'https://www.espn.com/espn/rss/news'),
        'https://www.espn.com/espn/rss/nfl/news',
        'https://www.espn.com/espn/rss/nba/news',
        'https://www.espn.com/espn/rss/mlb/news',
        'https://www.espn.com/espn/rss/nhl/news',
        'https://www.espn.com/espn/rss/golf/news',
        'https://www.espn.com/espn/rss/rpm/news',
        'https://www.espn.com/espn/rss/tennis/news',
        'https://www.espn.com/espn/rss/boxing/news',
        'https://www.espn.com/espn/rss/soccer/news',
        # 'http://soccernet.espn.go.com/rss/news',
        'https://www.espn.com/espn/rss/ncb/news',
        'https://www.espn.com/espn/rss/ncf/news',
        'https://www.espn.com/espn/rss/ncaa/news',
        # 'https://www.espn.com/espn/rss/outdoors/news',
        # 'http://sports.espn.go.com/espn/rss/bassmaster/news',
        'https://www.espn.com/espn/rss/oly/news',
        'https://www.espn.com/espn/rss/horse/news'
    ]

    def get_browser(self):
        br = BasicNewsRecipe.get_browser(self)
        if False and self.username and self.password:
            # ESPN has changed to a JS based login system, cant be bothered
            # revering it
            br.set_handle_refresh(False)
            url = ('https://r.espn.go.com/members/v3_1/login')
            raw = br.open(url).read()
            raw = re.sub(r'(?s)<form>.*?id="regsigninbtn".*?</form>', '', raw)
            with TemporaryFile(suffix='.htm') as fname:
                with open(fname, 'wb') as f:
                    f.write(raw)
                br.open_local_file(fname)

            br.form = list(br.forms())[0]
            br.form.find_control(
                name='username', type='text').value = self.username
            br.form['password'] = self.password
            br.submit().read()
            br.open('http://espn.go.com').read()
            br.set_handle_refresh(True)
        return br
